package com.ky.pro.java.sharding.ecommerce.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SerializeUtil {
    private static ObjectMapper objectMapper;

    /**
     * json字符串转JavaBean
     *
     * @param jsonStr
     * @param valueType
     * @return
     */
    public static <T> T toObject(String jsonStr, Class<T> valueType) {
        if (objectMapper == null) {
            createObjectMapper();
        }
        try {
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper.readValue(jsonStr, valueType);
        } catch (Exception e) {
            log.error("字符串:"+jsonStr+"反序列化异常:",e);
        }
        return null;
    }

    private static void createObjectMapper() {
        objectMapper = new ObjectMapper();
        objectMapper.findAndRegisterModules();
    }

    /**
     * json字符串转JavaBean
     *
     * @param jsonStr
     * @param valueTypeRef
     * @return
     */
    public static <T> T toObject(String jsonStr, TypeReference<T> valueTypeRef) {
        if (objectMapper == null) {
            createObjectMapper();
        }
        try {
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            return objectMapper.readValue(jsonStr, valueTypeRef);
        } catch (Exception e) {
            log.error("反序列化异常", e);
        }
        return null;
    }

    /**
     * 把JavaBean转换为json字符串
     *
     * @param object
     * @return
     */
    public static String toJson(Object object) {
        if (objectMapper == null) {
            createObjectMapper();
        }
        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("序列化异常", e);
        }

        return null;
    }
}
